﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.SessionManager;
using Business.BookingManager;

namespace Business
{
    public sealed class Authenticated
    {
        private static ISessionManager sessionManager =null;
        private static Authenticated instance = null;
        private static readonly object padlock = new object();

        public Authenticated() 
        {
            sessionManager = new SessionManagerClient();
            
        }
        public static Authenticated Instance
        {
            get
            {
                if(instance==null)
                {
                    lock (padlock)
                    {
                        if (instance == null)
                            instance = new Authenticated();
                    }
                }
                return instance;
            }
        }
        

        public string Login(string user,string password,string domain)
        {
            SessionManager.LogonRequest request = new SessionManager.LogonRequest();
            request.logonRequestData = new LogonRequestData();
            request.logonRequestData.AgentName = user;
            request.logonRequestData.Password = password;
            request.logonRequestData.DomainCode = domain;

            LogonResponse response;
            string signature=string.Empty;
            try
            {
                response = sessionManager.Logon(request);
                signature= response.Signature;
                return signature;
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            } 
        }

        public  bool Logout(string signature)
        {
            LogoutRequest request = new LogoutRequest();
            request.Signature = signature;
            LogoutResponse response;
            bool ok = false;

            try
            {
                response = sessionManager.Logout(request);
                if (response != null)
                    ok = true;
            }
            catch 
            {
                ok = false;
            }
            return ok;
        }
    }
}
